package org.csu.mypetstore.api.controller.front;

import org.csu.mypetstore.api.common.CommonResponse;
import org.csu.mypetstore.api.entity.CartItem;
import org.csu.mypetstore.api.entity.OrderStatus;
import org.csu.mypetstore.api.entity.Orders;
import org.csu.mypetstore.api.entity.Sequence;
import org.csu.mypetstore.api.service.CartService;
import org.csu.mypetstore.api.service.OrderService;
import org.csu.mypetstore.api.vo.AccountVO;
import org.csu.mypetstore.api.vo.CartVO;
import org.csu.mypetstore.api.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/order/")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private CartService cartService;

//    //init order
//    @PostMapping("initOrder")
//    @ResponseBody
//    public CommonResponse initOrder(HttpSession session){
//        AccountVO accountVO = (AccountVO) session.getAttribute("login_account") ;
//        session.setAttribute("login_account",accountVO);
//        return orderService.initOrder(accountVO);
//    }

//    //获取所有orders
//    @GetMapping("orders")
//    @ResponseBody
//    public CommonResponse<List<Orders>> getOrderList(){
//        return orderService.getOrderList();
//    }

    //由orderid获取单个order信息
    @GetMapping("orders/{orderId}")
    @ResponseBody
    public CommonResponse<OrderVO> getOrderByOrderId(@PathVariable("orderId") String orderId){
        Integer Id=Integer.parseInt(orderId);
        CommonResponse<OrderVO> response =orderService.getOrderByOrderId(Id);
        System.out.println(response.getData());
        return  response;
    }

    //new an 订单
    @PostMapping("newOrder")
    @ResponseBody
    public CommonResponse insertOrder(@RequestParam("orderVO") OrderVO orderVO, HttpSession session){
        AccountVO accountVO=(AccountVO)session.getAttribute("login_account");
        cartService.clear(accountVO.getUsername());
        OrderVO order=(OrderVO) orderService.initOrder(accountVO).getData();
        order.setBillAddress1(orderVO.getBillAddress1());
        order.setBillAddress2(orderVO.getBillAddress2());
        order.setBillCity(orderVO.getBillCity());
        order.setBillCountry(orderVO.getBillCountry());
        order.setBillState(orderVO.getBillState());
        order.setBillToFirstName(orderVO.getBillToFirstName());
        order.setBillToLastName(orderVO.getBillToLastName());
        order.setBillZip(orderVO.getBillZip());
        order.setCardType(orderVO.getCardType());
        order.setCourier(orderVO.getCourier());
        order.setCreditCard(orderVO.getCreditCard());
        order.setExpiryDate(orderVO.getExpiryDate());
        order.setShipZip(orderVO.getShipZip());
        order.setShipToFirstName(orderVO.getShipToFirstName());
        order.setShipToLastName(orderVO.getShipToLastName());
        order.setShipAddress1(orderVO.getShipAddress1());
        order.setShipAddress2(orderVO.getShipAddress2());
        order.setShipCity(orderVO.getShipCity());
        order.setShipCountry(orderVO.getShipCountry());
        order.setShipState(orderVO.getShipState());

        session.setAttribute("orderVO",order);
        session.removeAttribute("cartVO");
        return orderService.insertOrder(order);
    }

//    //查看未发货订单
//    @GetMapping("undeliverOrders")
//    @ResponseBody
//    public CommonResponse<List<OrderStatus>> getUndeliveredOrder(){
//        return  orderService.getUndeliveredOrder();
//    }
//
//    //查看已发货订单
//    @GetMapping("deliveredOrders")
//    @ResponseBody
//    public CommonResponse<List<OrderStatus>> getDeliveredOrder(){
//        return orderService.getDeliveredOrder();
//    }
//
//    //未发货订单发货
//    @PostMapping("undeliverOrders/{orderId}/deliver")
//    @ResponseBody
//    public CommonResponse Deliver(@PathVariable("orderId") String orderId){
//        Integer Id=Integer.parseInt(orderId);
//        return orderService.Deliver(Id);
//    }
//
//    //修改用户订单
//    @PostMapping("editOrder")
//    @ResponseBody
//    public CommonResponse editOrder(@RequestParam("orderVO") OrderVO orderVO, HttpSession session){
//        CommonResponse response = orderService.editOrder(orderVO);
//        session.setAttribute("orderVO", response.getData());
//        return response;
//    }
//
//    //搜索订单
//    @GetMapping("searchOrder")
//    @ResponseBody
//    public CommonResponse<List<Orders>> searchOrder(@RequestParam("keyword") String keyword, HttpSession session){
//        return orderService.searchOrder(keyword);
//    }
}
